package game.bejeweled
{
	import com.yzy.tool.Utils;
	
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	
	import effect.Effect;
	
	import event.ControlEvent;
	
	import game.monster.Monster;
	
	import models.BallModel;
	import models.BallPool;
	import models.ProfileModel;
	
	import starling.textures.Texture;
	
	import test.Test;
	
	

	public class CreateBalls extends EventDispatcher
	{
		private var block:BejeweledBlock;
		private var backdropTexture:Texture;
		private var ballPool:BallPool;
		//
		private var dropVeclength:int;//
		private var currNum:int;
		
		public function CreateBalls(block:BejeweledBlock,backdropTexture:Texture)
		{
			this.block=block;
			this.backdropTexture=backdropTexture;
			//
			ballPool=new BallPool();
			createBackDrops();
		}
		
		private function createBackDrops():void{
			for (var j:int = 0; j < ProfileModel.instant.row; j++) 
			{
				var columnVec:Vector.<BackDrop>=new Vector.<BackDrop>();
				for (var k:int = 0; k < ProfileModel.instant.column; k++) 
				{
					var backdrop:BackDrop=new BackDrop(backdropTexture);
					backdrop.y=j*backdrop.width;
					backdrop.x=k*backdrop.height;
					backdrop.rowNum=j;
					backdrop.column=k;
					block.addChild(backdrop);
					columnVec.push(backdrop);				
				}
				ProfileModel.instant.backdropVec.push(columnVec);
			}
		}
		
		public function sameBall():Vector.<Point>{
			return TestSameBall.sameBallVec();
		}
		
		
		
		
	
		
		/**
		 *  
		 * @param createDirection: "down" "up" "left" "right"
		 * 
		 */		
		public function createBall(createDirection:String="up"):void{
			//test
//			var l:String=Test.instant.createBallTestData.replace("\n\t\r");
//			var s:Array=l.split(",");
//			var i:int=s.length-1;
//			var randomNum:int=s[i++];
			//test
			var beginNum:int;
			var endNum:int;
			var upDown:int=0;
			var leftRight:int=0;
			var newBallX:Number;
			var newBallY:Number;
//			for (var r:int = ProfileModel.instant.row-1; r >=0; r--) 
//			{
//				for (var c:int = ProfileModel.instant.column-1; c >=0; c--) 
			for (var r:int = 0; r <ProfileModel.instant.row; r++) 
			{
				for (var c:int = 0; c <ProfileModel.instant.column; c++) 
				{
					var currBackDrop:BackDrop=ProfileModel.instant.backdropVec[r][c];
					if(!currBackDrop.isFull){
						var randomNum:int=com.yzy.tool.Utils.getRandom(0,ProfileModel.instant.ballModelVec.length-1);
//						var randomNum:int=1;
//						var randomNum:int=s[i--];
						var ballModale:BallModel=ProfileModel.instant.ballModelVec[randomNum];
						var newBall:Ball=BallPool.instant.getClass(ballModale);
						newBall.sign=randomNum;
						currBackDrop.currBall=newBall;
						
						var sideBackDrop:BackDrop=null;
						
						switch(createDirection)
						{
							case "down":
							{
								beginNum=r;
								endNum=0;
								upDown=-1;
								newBallX=Number(currBackDrop.ballX);
								newBallY=Number(currBackDrop.ballY-block.height);
								break;
							}
							case "up":
							{
								beginNum=r;
								endNum=ProfileModel.instant.row-1;
								upDown=1;
								newBallX=currBackDrop.ballX;
								newBallY=currBackDrop.ballY+block.height;
								break;
							}
							case "left":
							{
								beginNum=c+1;
								endNum=ProfileModel.instant.column;
								leftRight=1;
								newBallX=currBackDrop.ballX+block.width;
								newBallY=currBackDrop.ballY;
								break;
							}
							case "right":
							{
								beginNum=c-1;
								endNum=0;
								leftRight=-1;
								newBallX=currBackDrop.ballX-block.width;
								newBallY=currBackDrop.ballY;
								break;
							}
							default:
							{
								break;
							}
						}
						
						var currRow:int=r;
						var currColumn:int=c;
						while(beginNum!=endNum){
							beginNum+=upDown;
							beginNum+=leftRight;
							currRow+=upDown;
							currColumn+=leftRight;
							
							if(ProfileModel.instant.backdropVec[currRow][currColumn].isFull){
								sideBackDrop=ProfileModel.instant.backdropVec[currRow][currColumn];
								break;
							}
						}
						
						if(sideBackDrop!=null){
							currBackDrop.currBall=sideBackDrop.currBall;
							sideBackDrop.currBall=null;
							var moveBall:Ball=	sideBackDrop.currBall;
							var movetoP:Point=new Point(currBackDrop.ballX,currBackDrop.ballY);
						}else{
							
							newBall.x=newBallX;
							newBall.y=newBallY;
							block.addChild(newBall);
							moveBall=newBall;
							movetoP=new Point(currBackDrop.ballX,currBackDrop.ballY);
						}
						
						Effect.instant.addEventListener("moveball_complete",createComplete);
						Effect.instant.moveBallEffect(moveBall,movetoP);
					}
				}
			}
		}
		
		private function createComplete():void
		{
			this.dispatchEvent(new ControlEvent(ControlEvent.CREATE_BALL_COMPLETE));
		}		
		
		
		
		//************************************************************************
		// utils
		//***********************************************************************
		
		
		
	}
}

